package wyh.service;

import org.apache.ibatis.session.SqlSession;
import wyh.mapper.AppointmentMapper;
import wyh.pojo.Appointment;
import wyh.pojo.Appointment2;
import wyh.util.MyBatisUtils;

import java.util.List;

public class AppointmentService {

    /**需要移除 sqlSession.close(); 这行代码，因为 try-with-resources 语句会在 try 块结束时自动关闭 SqlSession
     * @return 关联名字的查询输出22222222
     */
    public List<Appointment2> ServicegetAll2() {
        try (SqlSession sqlSession = MyBatisUtils.getSession(true)) {
            AppointmentMapper appointmentMapper = sqlSession.getMapper(AppointmentMapper.class);
            return appointmentMapper.getAllAppointments2();
        }
    }

    public List<Appointment> ServicegetAll() {
        try (SqlSession sqlSession = MyBatisUtils.getSession(true)) {
            AppointmentMapper appointmentMapper = sqlSession.getMapper(AppointmentMapper.class);
            return appointmentMapper.getAllAppointments();
        }
    }

    public Appointment ServicegetById(int appointmentId) {
        try (SqlSession sqlSession = MyBatisUtils.getSession(true)) {
            AppointmentMapper appointmentMapper = sqlSession.getMapper(AppointmentMapper.class);
            return appointmentMapper.getAppointmentById(appointmentId);
        }
    }

    public boolean Serviceadd(Appointment appointment) {
        try (SqlSession sqlSession = MyBatisUtils.getSession(true)) {
            AppointmentMapper appointmentMapper = sqlSession.getMapper(AppointmentMapper.class);
            int result = appointmentMapper.insertAppointment(appointment);
            return result > 0;
        }
    }

    public boolean Serviceupdate(Appointment appointment) {
        try (SqlSession sqlSession = MyBatisUtils.getSession(true)) {
            AppointmentMapper appointmentMapper = sqlSession.getMapper(AppointmentMapper.class);
            int result = appointmentMapper.updateAppointment(appointment);
            return result > 0;
        }
    }

    public boolean ServicedeleteById(int appointmentId) {
        try (SqlSession sqlSession = MyBatisUtils.getSession(true)) {
            AppointmentMapper appointmentMapper = sqlSession.getMapper(AppointmentMapper.class);
            int result = appointmentMapper.deleteAppointment(appointmentId);
            return result > 0;
        }
    }
}